package com.example.douyin.friends;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.douyin.R;
import com.example.douyin.adapter.FriendsAdapter;
import com.example.douyin.databinding.FragmentFriendsBinding;
import com.example.lib_base_mvvm.baseMVVM.BaseView.BaseFragmnet;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;


public class FriendsFragment extends BaseFragmnet<FragmentFriendsBinding,FriendsViewModel> {

    List<String> usernames = new ArrayList<>();
    FriendsAdapter friendsAdapter;

    public FriendsFragment() {
        // Required empty public constructor
    }


    @Override
    public int bindLayout() {
        return R.layout.fragment_friends;
    }

    @Override
    public void initView() {
        //生成二维码图片--zll换成自己的用户名称
        dataBinding.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
                Bitmap image = CodeUtils.createImage("zll", 100, 100, bitmap);
                dataBinding.iv.setImageBitmap(image);
            }
        });

        dataBinding.rv.setLayoutManager(new LinearLayoutManager(getContext()));
        friendsAdapter = new FriendsAdapter(R.layout.item_friends,usernames);
        dataBinding.rv.setAdapter(friendsAdapter);
        //普通添加
        dataBinding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/app/AddFriendActivity").navigation();
            }
        });
        //点击二维码添加
        dataBinding.add2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CaptureActivity.class);
                startActivityForResult(intent,102);
            }
        });

        viewModel.friends();
        viewModel.success.observe(this, new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {
                usernames.clear();
                usernames.addAll(strings);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        friendsAdapter.notifyDataSetChanged();
                    }
                });

            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });


        friendsAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                String conversationId = usernames.get(position);
                int chatType = 0;
                Bundle bundle = new Bundle();
                bundle.putString("conversationId",conversationId);
                bundle.putInt("chatType",chatType);
                ARouter.getInstance().build("/app/ChatIMActivity").withBundle("bundle",bundle).navigation();
            }
        });

    }

    @Override
    public void initData() {
        viewModel.addSuccess.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
        viewModel.addFail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });

    }

    @Override
    public Class<FriendsViewModel> getViewModel() {
        return FriendsViewModel.class;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==102&&resultCode== Activity.RESULT_OK){
            String s = data.getExtras().getString(CodeUtils.RESULT_STRING);
            viewModel.addFriend(s,"通过二维码添加");
        }
    }
}